package com.example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;



/**
 * @author : H-Thien
 * @comment :
 */
public class Util {

	public Util() {

	}

	/**
	 * @comment :
	 * 
	 */
	public static boolean isNotNull(String s) {
		return s != null && !"".equals(s) && !"null".equals(s);
	}

	/**
	 * @comment :
	 * 
	 */
	public static boolean isNull(String s) {
		return s == null || "".equals(s);
	}

	/**
	 * @comment :
	 */
	public static String getAuthNum() {
		String authNum = String.valueOf(Math.random());

		return authNum.substring(authNum.length() - 8, authNum.length());
	}

	/**
	 * @comment :
	 * 
	 */
	public static String accountFlagChk(String s) {
		String accountNm = "";

		if ("1".equals(s)) {
			accountNm = Config.getInstance().getString("common.msg014"); // not
																			// account
		} else if ("2".equals(s)) {
			accountNm = Config.getInstance().getString("common.msg013"); // account
		}
		return accountNm;

	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String ReplaceAll(String s, String s1, String s2) {
		if (s == null) {
			return null;
		}

		if (s.indexOf(s1) < 0) {
			return s;
		}

		int i = s.length();
		int j = s1.length();
		StringBuffer stringbuffer = new StringBuffer();
		int k = 0;
		int l = 0;
		for (; k < i; k = l) {
			l = s.indexOf(s1, l);
			if (l < 0) {
				break;
			}
			stringbuffer.append(s.substring(k, l));
			stringbuffer.append(s2);
			l += j;
		}

		stringbuffer.append(s.substring(k));
		return stringbuffer.toString();
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String arrayToString(String as[]) {
		String s = new String();
		if (as != null) {
			for (int i = 0; i < as.length; i++) {
				if (i != as.length - 1) {
					s = s + as[i] + ",";
				} else {
					s = s + as[i];
				}
			}
		}
		return s;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getAlertMsg(String s) {
		s = ReplaceAll(s, "\n", "\\n");
		s = ReplaceAll(s, "\"", "'");
		return s;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getExt(String s) {
		if (s == null) {
			return "";
		}
		if (s.indexOf(".") != -1) {
			String s2 = s.substring(s.lastIndexOf("."));
			return s2;
		} else {
			return "";
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static Hashtable getParameter(HttpServletRequest httpservletrequest) {
		Enumeration enumeration = httpservletrequest.getParameterNames();
		Hashtable hashtable = new Hashtable();
		while (enumeration.hasMoreElements()) {
			String s = (String) enumeration.nextElement();
			String s1 = httpservletrequest.getParameter(s);
			if (s1 != null && !s1.equals("")) {
				hashtable.put(s, s1);
			}
		}
		return hashtable;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getParameter(HttpServletRequest httpservletrequest, String s) {
		String s1 = httpservletrequest.getParameter(s);
		if (s1 == null) {
			s1 = "";
		}
		return s1;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static Hashtable getParameterValues(HttpServletRequest httpservletrequest) {
		Enumeration enumeration = httpservletrequest.getParameterNames();
		Hashtable hashtable = new Hashtable();
		while (enumeration.hasMoreElements()) {
			String s = (String) enumeration.nextElement();
			String as[] = httpservletrequest.getParameterValues(s);
			if (as != null) {
				hashtable.put(s, as);
			}
		}
		return hashtable;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String[] getSplit(String s, String s1) {
		int i = 0;
		String as[] = null;
		if (s == null) {
			return null;
		}
		if (s != null && !s.equals("")) {
			StringTokenizer stringtokenizer = new StringTokenizer(s, s1);
			as = new String[stringtokenizer.countTokens()];
			while (stringtokenizer.hasMoreTokens()) {
				as[i++] = stringtokenizer.nextToken();
			}
		}
		return as;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static boolean isDate(String s) {
		if (s == null || s.length() != 8) {
			return false;
		}

		try {
			int i = Integer.parseInt(s.substring(0, 4));
			int j = Integer.parseInt(s.substring(4, 6));
			int k = Integer.parseInt(s.substring(6, 8));
			if (i < 1900 || i > 9999) {
				return false;
			}
			if (j < 1 || j > 12) {
				return false;
			}
			return k >= 1 && k <= 31;
		} catch (Exception _ex) {
			return false;
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static boolean isDateTime(String s) {
		if (s == null || s.length() != 14) {
			return false;
		}
		return isDate(s.substring(0, 8)) && isTime(s.substring(8, 14));
	}

	/**
	 * @method : isImageFile
	 * @date :
	 * @author : H-Thien
	 * @comment :
	 */
	public static boolean isImageFile(String s) {
		String s1 = getExt(s);
		return s1.equals(".gif") || s1.equals(".jpg") || s1.equals(".png") || s1.equals(".bmp");
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static boolean isTime(String s) {
		if (s == null || s.length() != 6) {
			return false;
		}

		try {
			int i = Integer.parseInt(s.substring(0, 2));
			int j = Integer.parseInt(s.substring(2, 4));
			int k = Integer.parseInt(s.substring(4, 6));
			if (i < 0 || i > 23) {
				return false;
			}
			if (j < 0 || j > 59) {
				return false;
			}
			return k >= 0 && k <= 59;
		} catch (Exception _ex) {
			return false;
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String nullTo(String s, String s1) {
		if (isNotNull(s)) {
			return s;
		} else {
			return s1;
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static int nullToInt(String s, String s1) {
		if (isNotNull(s)) {
			return Integer.parseInt(s);
		} else {
			return Integer.parseInt(s1);
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String[] stringToArray(String s, String s1) {
		if (isNull(s)) {
			return null;
		}
		StringTokenizer stringtokenizer = new StringTokenizer(s, s1);
		int i = stringtokenizer.countTokens();
		if (i <= 0) {
			return null;
		}
		String as[] = new String[i];
		for (int j = 0; j < i && stringtokenizer.hasMoreTokens(); j++) {
			as[j] = stringtokenizer.nextToken();
		}

		return as;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String uni2Ksc(String s) {
		if (s == null) {
			return null;
		}

		try {
			return new String(s.getBytes("8859_1"), "KSC5601");
		} catch (Exception _ex) {
			return null;
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String ksc2Uni(String s) {
		if (s == null) {
			return null;
		}

		try {
			return new String(s.getBytes("KSC5601"), "8859_1");
		} catch (Exception _ex) {
			return null;
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String euckrUni(String s) {
		if (s == null) {
			return null;
		}

		try {
			return new String(s.getBytes("euc-kr"), "ISO8859_1");
		} catch (Exception _ex) {
			return null;
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static void copy(String sin, String sout) throws IOException {
		FileInputStream in = new FileInputStream(sin);
		FileOutputStream out = new FileOutputStream(sout);
		synchronized (in) {
			synchronized (out) {
				byte[] buffer = new byte[256];
				while (true) {
					int byteRead = in.read(buffer);
					if (byteRead == -1) {
						break;
					}
					out.write(buffer, 0, byteRead);
				}
			}
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getMoneyText(String t) {
		t = nullTo(t, "0");

		int num = 0;
		String out = "";
		DecimalFormat a = new DecimalFormat("###,###,###,###,##0");
		num = Integer.parseInt(t);
		out = a.format(num);
		return out;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String makeButton(String name, String callMethod, String size) {
		String _CN_ = Config.getInstance().getString("context.name");
		StringBuffer tmpStr = new StringBuffer("");
		tmpStr.append("<div style=\"cursor:hand\" onClick=\"" + callMethod + "\">\r\n");
		tmpStr.append("<table width=\"" + size + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\r\n");
		tmpStr.append("<tr>\r\n");
		tmpStr.append("<td width=\"20\"><img src=\"" + _CN_ + "/img/common/btn_ccommon_left.jpg\" width=\"20\" height=\"21\"></td>\r\n");
		tmpStr.append("<td background=\"" + _CN_ + "/img/common/btn_ccommon_mid.jpg\"><div align=\"center\">" + name + "</div></td>\r\n");
		tmpStr.append("<td width=\"12\"><img src=\"" + _CN_ + "/img/common/btn_ccommon_right.jpg\" width=\"12\" height=\"21\"></td>\r\n");
		tmpStr.append("</tr>\r\n");
		tmpStr.append("</table>\r\n");
		tmpStr.append("</div>\r\n");

		return tmpStr.toString();
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getRequestURL(HttpServletRequest request) {
		String url = request.getRequestURI();
		boolean first = true;

		String key = null;
		String value = null;
		for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
			key = (String) e.nextElement();
			value = request.getParameter(key);
			if (isNotNull(value)) {
				url += first ? "?" + key + "=" + value : "&" + key + "=" + value;
			}
		}
		return url;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getParameterUrl(HttpServletRequest request) {
		String url = request.getRequestURI();
		boolean first = true;

		String key = null;
		String value = null;
		for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
			key = (String) e.nextElement();
			value = request.getParameter(key);
			if (isNotNull(value) && key.trim().equals("PAGE") == false) {
				url += first ? "?" + key + "=" + value : "&" + key + "=" + value;
				first = false;
			}
		}
		return url;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static double twoDownDecimal(double d) {
		d = (d * 100);
		d = (int) d;
		return d / 100.0;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String DetailEscape(String value) {
		String temp = value;

		if (temp.indexOf("&dol;") > -1) {
			temp = ReplaceAll(temp, "&dol;", "$");
		}
		if (temp.indexOf("&lt;") > -1) {
			temp = ReplaceAll(temp, "&lt;", "<");
		}
		if (temp.indexOf("&gt;") > -1) {
			temp = ReplaceAll(temp, "&gt;", ">");
		}
		if (temp.indexOf("&quot;") > -1) {
			temp = ReplaceAll(temp, "&quot;", "\"");
		}
		if (temp.indexOf("&amp;") > -1) {
			temp = ReplaceAll(temp, "&amp;", "&");
		}
		if (temp.indexOf("&nbsp;") > -1) {
			temp = ReplaceAll(temp, "&nbsp;", " ");
		}

		return temp;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String chgCommentString(String orgValue) {
		String temp = orgValue;

		if (temp.indexOf("'") > -1) {
			temp = ReplaceAll(temp, "'", "&#39");
		}
		if (temp.indexOf("\"") > -1) {
			temp = ReplaceAll(temp, "\"", "&quot;");
		}

		return temp;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String CReturn(String value) {
		String temp = value;

		if (temp.indexOf("<br/>") > -1) {
			temp = ReplaceAll(temp, "<br/>", "\r\n");
		}
		return temp;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String outConv(String orgStr) {
		if (orgStr == null) {
			return "";
		}

		orgStr = ReplaceAll(orgStr, "\n", "<br/>");
		orgStr = ReplaceAll(orgStr, "\r", "");
		return orgStr;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
//	public static String getPrimaryKey() {
//		Random rnd = new Random();
//
//		long rndVal = Math.abs(rnd.nextLong());
//		String curDate = DateUtil.getCurrentDateInfo("4");
//
//		return curDate + String.valueOf(rndVal);
//	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getOmittedString(String str, int len, String omitSymbol) {
		StringBuffer stringBuffer = new StringBuffer();

		String dot = "...";

		int counterChar = 0;
		int targetChar = 0;

		boolean targetCharCount = true;
		boolean isTwoByteCharacter = false;

		if (Util.isNull(str)) {
			return "";
		}

		if (Util.isNotNull(omitSymbol)) {
			dot = omitSymbol;
		}

		char[] strArray = str.toCharArray();

		for (int j = 0; j < strArray.length; j++) {
			isTwoByteCharacter = strArray[j] >= 44032 && strArray[j] <= 55203;

			if (isTwoByteCharacter) {
				counterChar += 2;
			} else {
				counterChar++;
			}

			if (counterChar >= len && targetCharCount) {
				targetChar = j;
				targetCharCount = false;
			}
		}

		if (targetChar > 0) {
			stringBuffer.append(str.substring(0, targetChar)).append(dot);
		} else {
			stringBuffer.append(str);
		}

		str = stringBuffer.toString();

		return str;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String[] splitString(String content, int splitSize) {
		Vector vt = new Vector(10, 10);
		String ctmp = content;
		int cntlen = 0;
		int csize = 3999;
		if (content != null) {
			ctmp = content;
			cntlen = content.getBytes().length;
		} else {
			ctmp = " ";
			cntlen = 1;
		}
		if (splitSize > 0) {
			csize = splitSize - 1;
		} else {
			csize = 3999;
		}

		while (cntlen > 0) {
			int bylen;
			int strlen = bylen = 0;
			if (cntlen > csize) {
				do {
					if (bylen >= csize) {
						break;
					}
					char c = ctmp.charAt(strlen);
					bylen++;
					strlen++;
					if (c > '\377') {
						bylen++;
					}
				} while (true);
			} else {
				strlen = ctmp.length();
				bylen = ctmp.getBytes().length;
			}
			cntlen -= bylen;
			String ct = ctmp.substring(0, strlen);
			ctmp = ctmp.substring(strlen);
			vt.add(ct);
		}
		String rtnArr[] = null;
		rtnArr = new String[vt.size()];
		vt.copyInto(rtnArr);
		return rtnArr;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String changeString(String str) {
		String temp = "";
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '\'') {
				temp = temp + "&#39;";
			} else if (str.charAt(i) == '"') {
				temp = temp + "&#34;";
			} else if (str.charAt(i) == '-') {
				temp = temp + "&#45;";
			} else if (str.charAt(i) == '<') {
				temp = temp + "&lt;";
			} else if (str.charAt(i) == '>') {
				temp = temp + "&gt;";
			} else if (str.charAt(i) == '%') {
				temp = temp + "&#37;";
			} else if (str.charAt(i) == '!') {
				temp = temp + "&#33;";
			} else {
				temp = temp + str.charAt(i);
			}
		}
		return temp;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getComma(String str) {
		return getComma(str, true);
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getComma(String str, boolean isTruncated) {
		if (str == null) {
			return "";
		}
		if (str.trim().equals("")) {
			return "";
		}
		if (str.trim().equals("&nbsp;")) {
			return "&nbsp;";
		}
		int pos = str.indexOf(".");
		if (pos != -1) {
			if (!isTruncated) {
				DecimalFormat commaFormat = new DecimalFormat("#,##0.00");
				return commaFormat.format(Float.parseFloat(str.trim()));
			} else {
				DecimalFormat commaFormat = new DecimalFormat("#,##0");
				return commaFormat.format(Long.parseLong(str.trim().substring(0, pos)));
			}
		} else {
			DecimalFormat commaFormat = new DecimalFormat("#,##0");
			return commaFormat.format(Long.parseLong(str.trim()));
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getComma(Long lstr) {
		DecimalFormat commaFormat = new DecimalFormat("#,##0");
		if (lstr == null) {
			return "";
		} else {
			return commaFormat.format(lstr);
		}
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getZeroBaseString(int num, int size) {
		return getZeroBaseString(String.valueOf(num), size);
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getZeroBaseString(String num, int size) {
		StringBuffer strBuf = new StringBuffer();
		if (num.length() >= size) {
			return num;
		}
		for (int index = 0; index < size - num.length(); index++) {
			strBuf.append("0");
		}

		return strBuf.toString() + num;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static int getByteSize(String str) {
		int counterChar = 0;
		boolean isTwoByteCharacter = false;

		char[] strArray = str.toCharArray();

		for (int j = 0; j < strArray.length; j++) {
			isTwoByteCharacter = strArray[j] >= 44032 && strArray[j] <= 55203;

			if (isTwoByteCharacter) {
				counterChar += 2;
			} else {
				counterChar++;
			}
		}

		return counterChar;
	}

	/**
	 * @author : H-Thien
	 * @date :
	 * @comment :
	 */
	public static String getZipcode(String s) {
		String zcode = "";
		try {
			zcode += s.substring(0, 3);
			zcode += s.substring(3, 7);
		} catch (Exception e) {
			return s;
		}

		return zcode;
	}

	/**
	 * @author : loto winner e-mail title
	 * @date :
	 * @comment :
	 */
//	public static String makeTitle(PopUpEntity lme) throws Throwable {
//		StringBuffer sbTitle = new StringBuffer();
//		sbTitle.append(Config.getInstance().getString("lotonomng.title045"));
//		sbTitle.append(" ");
//		sbTitle.append(Config.getInstance().getString("lotonomng.title024"));
//		sbTitle.append(" ");
//		sbTitle.append(lme.getLoto_order());
//		sbTitle.append(Config.getInstance().getString("lotonomng.title025"));
//		sbTitle.append(" ");
//		sbTitle.append(Config.getInstance().getString("lotonomng.title026"));
//		sbTitle.append(" ");
//		sbTitle.append(lme.getLoto_type_nm());
//		sbTitle.append(" ");
//		sbTitle.append(lme.getRank());
//		sbTitle.append(Config.getInstance().getString("lotonomng.title028"));
//
//		return sbTitle.toString();
//	}

	/**
	 * @author : loto winner e-mail content
	 * @date :
	 * @comment :
	 */
//	public static String makeHtml(PopUpEntity lme) throws Throwable {
//
//		StringBuffer contentSb = new StringBuffer();
//		contentSb.append(Config.getInstance().getString("lotonomng.title029") + "<br/>");
//		contentSb.append(lme.getLoto_order());
//		contentSb.append(Config.getInstance().getString("lotonomng.title025"));
//		contentSb.append("&nbsp;");
//		contentSb.append(Config.getInstance().getString("lotonomng.title026"));
//		contentSb.append("&nbsp;");
//		contentSb.append(lme.getLoto_type_nm());
//		contentSb.append("&nbsp;");
//		contentSb.append(lme.getRank());
//		contentSb.append(Config.getInstance().getString("lotonomng.title030") + "<br/><br/>");
//		contentSb.append("※ " + Config.getInstance().getString("lotonomng.title031") + "<br/>");
//		contentSb.append("- " + Config.getInstance().getString("lotonomng.title032") + " : ");
//		contentSb.append("[" + lme.getLoto_order() + Config.getInstance().getString("lotonomng.title025") + "] " + lme.getEntry_num() + "<br/>");
//		contentSb.append("- " + Config.getInstance().getString("lotonomng.title033") + " : ");
//		contentSb.append(lme.getRank() + Config.getInstance().getString("lotonomng.title047") + " / " + Util.getMoneyText(lme.getPayout()) + Config.getInstance().getString("lotonomng.title048")
//				+ "<br/>");
//		contentSb.append("- " + Config.getInstance().getString("lotonomng.title034") + " : ");
//		contentSb.append(lme.getAd_name() + "<br/>");
//		contentSb.append("- " + Config.getInstance().getString("lotonomng.title035") + " : ");
//		contentSb.append(lme.getComp_name() + "<br/><br/>");
//		contentSb.append("※ " + Config.getInstance().getString("lotonomng.title036") + "<br/>");
//		contentSb.append("- " + Config.getInstance().getString("lotonomng.title037") + "<br/>");
//		contentSb.append("- " + Config.getInstance().getString("lotonomng.title038") + "<br/><br/>");
//		contentSb.append(Config.getInstance().getString("lotonomng.title042") + "<br/>");
//		contentSb.append(Config.getInstance().getString("lotonomng.title043") + "<br/>");
//
//		return contentSb.toString();
//	}
}